﻿
using Newtonsoft.Json;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Controllers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Controllers
{
    public class TemplateController : BaseController
    {
        private TemplateServers templateServers;

        public TemplateController()
        {
            templateServers = new TemplateServers();
        }
        
        // GET: System/Template
        public ActionResult Index()
        {
            ViewBag.TemplateKindData = JsonConvert.SerializeObject(templateServers.GetTemplateKind());
            return View();
        }

        // GET: System/Template/Edit/5
        public ActionResult Edit(int? id)
        {
            List<SelectListItem> typelist = new List<SelectListItem>();
            typelist.Add(new SelectListItem { Value = "", Text = "--请选择--" });
            typelist.Add(new SelectListItem { Value = "1", Text = "否" });
            typelist.Add(new SelectListItem { Value = "2", Text = "是" });
            ViewBag.TYPEData = typelist;
            ViewBag.TemplateKindData = templateServers.GetTemplateKind();
            var model = new template();
            if (id != null)
            {
                ViewBag.Title = "编辑模板";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建模板";
            }
            return View(model);
        }

        // POST: System/Template/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, template model)
        {
            List<SelectListItem> typelist = new List<SelectListItem>();
            typelist.Add(new SelectListItem { Value = "", Text = "--请选择--" });
            typelist.Add(new SelectListItem { Value = "1", Text = "否" });
            typelist.Add(new SelectListItem { Value = "2", Text = "是" });
            ViewBag.TYPEData = typelist;
            ViewBag.TemplateKindData = templateServers.GetTemplateKind();
            try
            {
                if (id != null)
                {
                    templateServers.Update(model);
                    ViewBag.msg = "修改模板信息成功！";
                }
                else
                {
                    templateServers.Add(model);
                    ViewBag.msg = "新增模板信息成功！";
                    model = new template();
                }
            }
            catch (Exception ex)
            {
                ViewBag.msg = "提交出现错误(" + ex.ToString() + ")！";
            }

            return View(model);
        }

        public void SaveDoc(string filePath)
        {
            PageOffice.FileSaver fs = new PageOffice.FileSaver();
            fs.SaveToFile(Server.MapPath(filePath));
            fs.Close();
        }
        public ActionResult Detail(string filePath)
        {
            if(string.IsNullOrEmpty(filePath))
            {
                ViewBag.ContentHtml = "请先上传模板文件！";
            }
            else
            {
                string suffix = Path.GetExtension(filePath);

                PageOffice.PageOfficeCtrl pc = new PageOffice.PageOfficeCtrl();
                pc.AddCustomToolButton("保存", "Save()", 1);
                pc.AddCustomToolButton("打印", "ShowPrintDlg()", 6);
                pc.AddCustomToolButton("-", "", 0);
                pc.AddCustomToolButton("全屏切换", "SwitchFullScreen()", 4);
                pc.SaveFilePage = "/System/Template/SaveDoc?filePath=" + Url.Encode(filePath);
                pc.ServerPage = "/PageOffice/server.aspx";
                switch (suffix.ToLower())
                {
                    case ".doc":
                    case ".docx":
                        pc.WebOpen(filePath, PageOffice.OpenModeType.docReadOnly, "s");
                        break;
                    case ".xls":
                    case ".xlsx":
                        pc.WebOpen(filePath, PageOffice.OpenModeType.xlsReadOnly, "s");
                        break;
                    default:
                        break;
                }
                ViewBag.ContentHtml = pc.GetHtmlCode("PageOfficeCtrl1");
            }
            
            return View();
        }

        // GET: System/Template/Delete/5
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                templateServers.Del(id.ToString());
                ViewBag.msg = "删除模板信息成功！";
            }
            return Content("");
        }


        #region //GetData
        public string GetTableJsonResult()
        {
            var list = templateServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public template GetModel(string id)
        {
            var model = templateServers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}